import localforage from 'localforage';
import { mockUsers, mockArticles, mockTags, mockComments, mockCarouselSlides } from '@/mock/data';
import type { Article, User, Tags, Comment } from '@/types';
import type { CarouselSlide } from '@/types'
// 初始化本地数据
localforage.config({
  name: 'teach-demo',
  version: 1.0,
  storeName: 'teach-data',
  description: '用于存储一些测试数据'
});

/**
 * @description 初始化数据库数据
 */
export const initializeDatabase = async (): Promise<void> => {
  try {
    const carouselSlides = await localforage.getItem('mockCarouselSlides');
    if (!carouselSlides) {
      await localforage.setItem('mockCarouselSlides', mockCarouselSlides);
    }
    const articles = await localforage.getItem('mockArticles');
    if (!articles) {
      await localforage.setItem('mockArticles', mockArticles);
    }
    const users = await localforage.getItem('mockUsers');
    if (!users) {
      await localforage.setItem('mockUsers', mockUsers);
    }
    const tags = await localforage.getItem('mockTags');
    if (!tags) {
      await localforage.setItem('mockTags', mockTags);
    }
    const comments = await localforage.getItem('mockComments');
    if (!comments) {
      await localforage.setItem('mockComments', mockComments);
    }
  } catch (error) {
    console.error('数据库初始化失败:', error);
  }
};
/**
 * @description 获取用户数据
 */
export const getMockUsers = async (): Promise<User[] | null> => {
  return localforage.getItem('mockUsers');
};

/**
 * @description 获取所有文章数据
 */
export const getMockAllArticles = async (): Promise<Article[] | null> => {
  return localforage.getItem('mockArticles');
};

/**
 * @description 获取所有标签数据
 */

export const getMockAllTags = async (): Promise<Tags[] | null> => {
  return localforage.getItem('mockTags');
};

/**
 * @description 获取所有轮播图数据
 */
export const getMockCarousals= async():Promise<CarouselSlide[]|null>=>{
    return localforage.getItem('mockCarouselSlides');
}
/**
 * @description 获取评论数据
 */

export const getMockComments = async (): Promise<Comment[] | null> => {
  return localforage.getItem('mockComments');
};

/**
 * @description 更新文章数据
 */
export const updateMockArticles = async (
  articles: Article[]
): Promise<void> => {
  await localforage.setItem('mockArticles', articles);
};

/**
 * @description 更新单个文章
 */
export const updateMockArticle = async (
  articleId: string,
  updatedArticle: Partial<Article>
): Promise<void> => {
  const articles = (await localforage.getItem('mockArticles')) as Article[];
  if (articles) {
    const updatedArticles = articles.map((article) =>
      article.id === articleId ? { ...article, ...updatedArticle } : article
    );
    await localforage.setItem('mockArticles', updatedArticles);
  }
};

/**
 * @description 更新用户数据
 */
export const updateMockUsers = async (users: User[]): Promise<void> => {
  await localforage.setItem('mockUsers', users);
};

/**
 * @description 更新标签数据
 */
export const updateMockTags = async (tags: Tags[]): Promise<void> => {
  await localforage.setItem('mockTags', tags);
};

/**
 * @description 更新评论数据
 */
export const updateMockComments = async (
  comments: Comment[]
): Promise<void> => {
  await localforage.setItem('mockComments', comments);
};

/**
 * @description 添加新评论
 */
export const addMockComment = async (comment: Comment): Promise<void> => {
  const comments = await getMockComments();
  const updatedComments = comments ? [comment, ...comments] : [comment];
  await updateMockComments(updatedComments);
};

/**
 * @description 更新评论点赞数
 */
export const updateCommentLikes = async (
  commentId: string,
  likes: number
): Promise<void> => {
  const comments = await getMockComments();
  if (comments) {
    const updatedComments = comments.map((comment) => {
      // 如果是主评论
      if (comment.id === commentId) {
        return { ...comment, likes };
      }
      // 如果是子评论，递归查找并更新
      if (comment.replies) {
        const updatedReplies = comment.replies.map((reply) =>
          reply.id === commentId ? { ...reply, likes } : reply
        );
        return { ...comment, replies: updatedReplies };
      }
      return comment;
    });
    await updateMockComments(updatedComments);
  }
};

/**
 * @description 添加文章收藏
 */
export const addArticleStar = async (articleId: string): Promise<void> => {
  const articles = await getMockAllArticles();
  if (articles) {
    const updatedArticles = articles.map((article) =>
      article.id === articleId
        ? { ...article, stars: (article.stars || 0) + 1 }
        : article
    );
    await updateMockArticles(updatedArticles);
  }
};

/**
 * @description 取消文章收藏
 */
export const cancelArticleStar = async (articleId: string): Promise<void> => {
  const articles = await getMockAllArticles();
  if (articles) {
    const updatedArticles = articles.map((article) =>
      article.id === articleId
        ? { ...article, stars: (article.stars || 0) - 1 }
        : article
    );
    await updateMockArticles(updatedArticles);
  }
};

/**
 * @description 添加文章点赞
 */
export const addArticleLike = async (articleId: string): Promise<void> => {
  const articles = await getMockAllArticles();
  if (articles) {
    const updatedArticles = articles.map((article) =>
      article.id === articleId
        ? { ...article, likes: (article.likes || 0) + 1 }
        : article
    );
    await updateMockArticles(updatedArticles);
  }
};
/**
 * @description 取消文章点赞
 */
export const cancelArticleLike = async (articleId: string): Promise<void> => {
  const articles = await getMockAllArticles();
  if (articles) {
    const updatedArticles = articles.map((article) =>
      article.id === articleId
        ? { ...article, likes: (article.likes || 0) - 1 }
        : article
    );
    await updateMockArticles(updatedArticles);
  }
};
